<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="drawer = true">新增</el-button>
        <el-button type="success">高级搜索</el-button>
      </el-col>
      <el-col :span="12">
        <el-input
          placeholder="请输入内容"
          prefix-icon="el-icon-search"
          v-model="input"
        >
        </el-input
      ></el-col>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%; margin-top: 15px">
      <el-table-column type="index" label="#" width="180"> </el-table-column>
      <el-table-column prop="title" label="题目" width="400"> </el-table-column>
      <el-table-column prop="uid" label="分类" width="180"> </el-table-column>
      <el-table-column prop="cid" label="作者"> </el-table-column>
      <el-table-column prop="created" label="发布时间" width="180">
      </el-table-column>
      <el-table-column width="300" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="updateFun(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteFun(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination small layout="prev, pager, next" :total="50">
    </el-pagination>
    <el-drawer
      title="我是标题"
      size="60%"
      :visible.sync="drawer"
      :with-header="false"
    >
      <el-page-header content="详情页面"></el-page-header>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="题目">
          <el-input
            type="textarea"
            autosize
            :show-word-limit="true"
            v-model="form.title"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="form.cid" placeholder="请选择活动区域">
            <el-option
              v-for="(item, index) in edit"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选项">
          <div class="dis" v-for="(item, index) in initOptions" :key="index">
            <el-checkbox
              v-model="item.checked"
              :label="item.text"
            ></el-checkbox>
            <el-input
              type="textarea"
              :show-word-limit="true"
              autosize
              v-model="item.value"
            ></el-input>
            <el-button
              type="primary"
              icon="el-icon-plus"
              v-if="index == initOptions.length - 1"
              circle
              @click="pushInitOptions"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-minus"
              v-if="index > 3"
              circle
              @click="updateInitOptions(index)"
            ></el-button>
          </div>
        </el-form-item>
        <el-form-item label="答案">
          <div>{{ doSort && doSort.join(",") }}</div>
        </el-form-item>
        <el-form-item label="答题提示">
          <el-input
            type="textarea"
            autosize
            :show-word-limit="true"
            v-model="form.tips"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitFun">保存</el-button>
          <el-button type="success" @click="offFun">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { localClassify } from "../../utils/local/local";
import { mapState, mapActions } from "vuex";
export default {
  layout: "mine",
  data() {
    return {
      input: "",
      // tableData: [{}],
      drawer: false,
      edit: localClassify.save(),
      form: {
        options: [],
      },
      initOptions: [
        { text: "A", value: "" },
        { text: "B", value: "" },
        { text: "C", value: "" },
        { text: "D", value: "" },
      ],
    };
  },
  mounted() {
    this.getquestions();
  },
  computed: {
    ...mapState("study", ["tableData"]),
    doSort() {
      let tru = this.initOptions.filter((v) => {
        return v.checked;
      });
      return tru.map((v, i) => v.text);
      // return this.form.options.sort((a, b) => a.localeCompare(b));
    },
  },
  methods: {
    ...mapActions("study", [
      "getquestions",
      "addquestions",
      "deletequestions",
      "updatequestions",
    ]),
    async deleteFun(id) {
      this.$confirm("此操作将永久删除该类别, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await this.deletequestions({ id });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    updateFun(row) {
      this.form = row;
      // this.doSort = JSON.parse(this.form.answers);
      this.initOptions = JSON.parse(this.form.options);
      console.log(this.form);
      this.drawer = true;
    },
    async submitFun() {
      if (this.form.id) {
        await this.updatequestions({
          ...this.form,
          answers: JSON.stringify(this.doSort),
          options: JSON.stringify(this.initOptions),
        })
      } else {
        await this.addquestions({
          ...this.form,
          answers: JSON.stringify(this.doSort),
          options: JSON.stringify(this.initOptions),
        });
      }
      this.getquestions();
      this.offFun();
    },
    offFun() {
      this.form = { options: [] };
      this.drawer = false;
    },
    pushInitOptions() {
      // 65-90 A-Z
      for (let index = 68; index < 91; index++) {
        if (
          this.initOptions[this.initOptions.length - 1].text ==
          String.fromCharCode(index)
        ) {
          this.initOptions.push({
            text: String.fromCharCode(index + 1),
            value: "",
          });
          return;
        }
      }
    },
    updateInitOptions(ind) {
      this.initOptions.splice(ind, 1);
      for (let ads = 0; ads < this.initOptions.length; ads++) {
        this.initOptions[ads].text = String.fromCharCode(ads + 65);
      }
    },
  },
};
</script>

<style scoped>
.dis {
  display: flex;
}
.el-textarea {
  width: 200px;
}
.dis > .el-textarea {
  padding-left: 10px;
}
</style>